<template>
  <div class="main-content no-padding">
    <div class="container-fluid">
      <div class="panel">
        <div class="panel-body">
          <el-row>
            <el-col :span="14" :offset="5">
              <el-input placeholder="编码或名称" v-model="searchText" class="input-with-select">
                <template #append>
                  <el-button icon="el-icon-search" @click="search"></el-button>
                </template>
              </el-input>
            </el-col>
            <el-col :span="5">
              <el-button class="pull-right" @click="showAddDialog">添加</el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="panel">
        <el-table
          :data="rulesEngineList"
          stripe
          style="width: 100%">
          <el-table-column type="expand">
            <template #default="props">
              <pre>
                {{ props.row.data }}
              </pre>
            </template>
          </el-table-column>
          <el-table-column
            width="50px"
            label="ID"
            prop="id">
          </el-table-column>
          <el-table-column
            width="200px"
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="描述"
            prop="description">
          </el-table-column>
          <el-table-column
            width="200px"
            label="创建时间"
            prop="createTime">
          </el-table-column>
          <el-table-column
            width="200px"
            label="更新时间"
            prop="updateTime">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template #default="scope">
              <el-button type="text" size="small">编辑</el-button>
              <el-button type="text" @click="openFlowEditor(scope.row)" size="small">编辑流程</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          v-model:current-page="page.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          v-model:page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          v-model:total="page.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="添加规则引擎"
      v-model="dialogAddVisible"
      width="50%">
      <el-form label-position="left" label-width="120px" :model="rulesEngineConfig">
        <el-form-item label="名称">
          <el-input v-model="rulesEngineConfig.name" placeholder="一个名称，方便查看用的"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="rulesEngineConfig.description"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogAddVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRulesEngine">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {addRulesEngineConfig, fetchRulesEngineConfig} from '@/api/engine/rulesEngineConfig'

export default {
  name: "BaseRulesEngineConfig",
  data() {
    return {
      searchText: "",
      dialogAddVisible: false,
      rulesEngineList: [],
      rulesEngineConfig: {},
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
    }
  },
  methods: {
    handleSizeChange() {
      this.search()
    },
    handleCurrentChange() {
      this.search()
    },
    showAddDialog() {
      this.dialogAddVisible = true
      this.rulesEngineConfig = {}
    },
    addRulesEngine() {
      let that = this
      addRulesEngineConfig(that.rulesEngineConfig).then(() => {
        that.dialogAddVisible = false
        that.search()
      })
    },
    openFlowEditor: function (row) {
      let routerJump = this.$router.resolve({name: 'rulesEngineFlow', query: {id: row.id}});
      window.open(routerJump.href, '_blank');
    },
    search: function () {
      let that = this
      fetchRulesEngineConfig(that.page.currentPage, that.page.pageSize).then(response => {
        that.rulesEngineList = response.data.dataList
        that.page.total = response.data.totalCount
      })
    }
  },
  mounted() {
    this.search()
  }
}
</script>

<style scoped>

</style>
